package cn.pay.http;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

/**
 * 网络访问处理类，目前只有POST请求
 *
 */
public class GPayHttp extends AsyncTask<String , Void, String>{
	
	private static final int TIMEOUT_CONNECTION = 15000;//设置超时时间
	private GPayHttpListener httpListener;//网络访问回调接口
	private Context mContext;
	private String getDatas;//请求数据
	private int responseCode;//状态码

	public GPayHttp(Context context){
		mContext=context;
	}
	
	
	//post 请求数据初始化
	public void doPost(GPayHttpListener GPayHttpListener, String params, String apiUrl,String path) {	
		
		httpListener = GPayHttpListener;
		getDatas = params;
		String httpURL = apiUrl + path;//请求地址拼接
		execute(new String[] { httpURL });

	}

	private String executeHttp(Context context, String url) {
		//返回什么类型请求
		return  post(context, url);

	}
	
	@Override
	protected void onPreExecute() {
	
	}
	
	

	@Override
	protected void onPostExecute(String response) {
		super.onPostExecute(response);
		
		
		
		
		if ((httpListener != null) && (!isCancelled())) {
			if (responseCode ==200) {
				httpListener.onSuccess(response);

			} else {

				httpListener.onFailure(responseCode);

			}
			httpListener = null;
		}

	}

	@Override
	protected void onCancelled() {

		super.onCancelled();

		if (httpListener != null) {
			httpListener = null;
		}

	}

	@Override
	protected String doInBackground(String... params) {

		String response = null;
		try {
			String url = params[0];
			response = executeHttp(mContext, url);
		} catch (Exception e) {
		//	MLog.e(TAG, "  doInBackground http error !"+e.getMessage());
		}

		return response;
	}
	
	/**
	 * @return 服务器返回数据
	 * 
	 */
	private  String post(Context mContext, String httpUrl) {
		
		String response = null;
		HttpURLConnection connection = null;
		try {
			URL url = new URL(httpUrl);
			connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setRequestMethod("POST");
			connection.setReadTimeout(TIMEOUT_CONNECTION);
			connection.setConnectTimeout(TIMEOUT_CONNECTION);
			connection.setRequestProperty("Content-Type", "application/json");

			OutputStream outputStream = connection.getOutputStream();
			outputStream.write(getDatas.getBytes());
			outputStream.flush();
			outputStream.close();
			int resCode = connection.getResponseCode();
			responseCode=resCode;

			if (resCode == 200) {
				response = IOUtils.toString(connection.getInputStream());
			} 

		} catch (Exception e) {
		//	MLog.e(TAG, " post http error !"+e.getMessage());
		} finally {
			if (connection != null)
				connection.disconnect();
		}
		return response;
	}
	
	

	/**
	 * @param mContext 上下文
	 * @param httpUrl 地址
	 * 	获取图片
	 * 
	 */
	public   static Bitmap doGet(Context mContext,String httpUrl){
		
		Bitmap bitmap=null;
		try {
		
			HttpGet httpRequest = new HttpGet(httpUrl);
			HttpClient httpclient = new DefaultHttpClient();

			// 请求httpClient ，取得HttpRestponse
			HttpResponse httpResponse = httpclient.execute(httpRequest);
			int resCode=httpResponse.getStatusLine().getStatusCode();
			if (resCode== 200) {
				
				HttpEntity httpEntity = httpResponse.getEntity();
				// 获得一个输入流
				InputStream is = httpEntity.getContent();
				bitmap = BitmapFactory.decodeStream(is);
				is.close();

			}

		} catch (Exception e) {
			//MLog.e(TAG, " get http error !"+e.getMessage());
		}

		return bitmap;

	}

}
